<?php

namespace app\beautiful\controller\admin\project;

use app\BaseController;

use app\beautiful\model\mall\GoodsCategoryModel;
use app\common\logic\DataLogic;

use app\beautiful\model\project\ProjectModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\project\ProjectTypeModel;
use think\Model;


class Project extends BaseController{

    protected $name='项目';

    

    public function index()

    {

        $abbreviation=$this->request->param('abbreviation');

        $project_type_id=$this->request->param('project_type_id/d');

        $bg_take_count=$this->request->param('bg_take_count/d');

        $end_take_count=$this->request->param('end_take_count/d');

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

        ];



        if(!empty($bg_take_count)){

            if(!empty($end_take_count)){

                $where[]=['take_count','between',[$bg_take_count,$end_take_count]];

            }else{

                $where[]=['take_count','>=',$bg_take_count];

            }

        }else{

            if(!empty($end_take_count)){

                $where[]=['take_count','<=',$end_take_count];

            }

        }



        if(!empty($abbreviation)) $where[]=['abbreviation','=',$abbreviation];

        if(!empty($project_type_id)) $where[]=['project_type_id','=',$project_type_id];

        $project_model= new ProjectModel();

        $project_model=$project_model->with(['type']);

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['add_time_format'];

        $return = DataLogic::getDataList($project_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);



        if($this->request->page==1){

            $type_list=ProjectTypeModel::where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0]

            ])->field('project_type_id,name')->select()->toArray();

            $return['type_list']=$type_list;

        }

        //处理数据
        $lists = $return['list'];
        if(empty(!$lists)){

            $key_start = $this->request->limit * ($this->request->page - 1);
            $key_end = $return['total'];

            foreach ($lists as $k => $list){
                $lists[$k]['show_project_id']= $key_end - ($key_start + $k);
            }
        }
        $return['list']=$lists;

        return $this->success($return);



    }



    public function show()

    {

        $return=[];

        $project_id = $this->request->param('project_id/d',0);

        $type_list=ProjectTypeModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0]

        ])->field('project_type_id,name')->select()->toArray();

        $return['type_list']=$type_list;

        if($project_id==0) return $this->success($return);



        if(null === $project = ProjectModel::find($project_id)) return $this->error($this->name.'不存在');

        if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

        if($project->is_delete != 0) return $this->error($this->name.'不存在');



        $detail=[

            'project_type_id'=>$project->project_type_id,

            'full_name'=>$project->full_name,

            'abbreviation'=>$project->abbreviation,

            'market_price'=>$project->market_price,

            'gross'=>$project->gross,

            'cover_img'=>$project->cover_img,

        ];



        $return['detail']=$detail;

        return $this->success($return);



    }



    public function save()

    {

        $project_id = $this->request->param('project_id/d',0);

        $full_name=$this->request->param('full_name');

        $project_type_id=$this->request->param('project_type_id/d',0);

        $abbreviation=$this->request->param('abbreviation');

        $market_price=$this->request->param('market_price/f',0);

        $gross=$this->request->param('gross/f',0);

        $cover_img=$this->request->param('cover_img');



        if(empty($full_name)) return $this->error('请填写全称');

        if(empty($abbreviation)) return $this->error('请填写简称');

        if(empty($project_type_id)) return $this->error('请选择分类');

        if(null === $project_type =  ProjectTypeModel::find($project_type_id)) return $this->error('请选择分类');

        if(empty($market_price) || $market_price<=0) return $this->error('请输入市场价，且不能小于0');

        if(empty($gross) || $gross<=0) return $this->error('请输入毛利，且不能小于0');

        if(empty($cover_img)) return $this->error('请上传封面');



        if($project_id!=0){

            if(null === $project = ProjectModel::find($project_id)) return $this->error($this->name.'不存在');

            if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

            if($project->is_delete != 0) return $this->error($this->name.'不存在');

            $pre_cover_img=$project->cover_img;

        }else{

            $project = new ProjectModel();

            $project->shop_id = $this->request->shopId;

            $pre_cover_img=null;

            //复制到商品表 20220529 @robote
            //创建“项目”分类
            $goods_category =  GoodsCategoryModel::where('name','项目')
                ->where('shop_id',$this->request->shopId)
                ->where('is_delete',0)
                ->find();

            if($goods_category == null){
                $goods_category = new GoodsCategoryModel();
                $goods_category->shop_id = $this->request->shopId;
                $goods_category->name='项目';
                $goods_category->pid=0;
                $goods_category->icon='';
                $goods_category->sort=1;
                $goods_category->save();
            }
            //项目名称：
            $project_type_name = $project_type->name;
            $goods_sub_category_name = $project_type_name.'(项目)';

            $sub_category =  GoodsCategoryModel::where('name',$goods_sub_category_name)
                ->where('shop_id',$this->request->shopId)
                ->where('is_delete',0)
                ->find();

            if($sub_category  == null){
                $sub_category = new GoodsCategoryModel();
                $sub_category->shop_id = $this->request->shopId;
                $sub_category->name=$goods_sub_category_name;
                $sub_category->pid=$goods_category->category_id;
                $sub_category->icon='';
                $sub_category->sort=1;
                $sub_category->save();
            }


            $goods = new GoodsModel();

            $goods_details=[];
            $rotation = [$cover_img];
            $pre_cover_img=null; $pre_rotation=[]; $pre_goods_details=[];

            $goods->shop_id = $this->request->shopId;
            $goods->name = $full_name."(项目)";
            $goods->code= 'A001';
            $goods->category_id=$sub_category->category_id;
            $goods->original_price=$market_price;
            $goods->vip_price=$market_price;
            $goods->gross=$gross;
            $goods->quota=100;
            $goods->cover_img=$cover_img;
            $goods->rotation=$rotation;
            $goods->goods_details= $goods_details;
            $goods->purchase_information="购买须知";
            $goods->update_time=$this->request->time();
            $goods->status=1;
            $goods->sort=100;
            $goods->save();


        }



        $project->full_name=$full_name;

        $project->abbreviation=$abbreviation;

        $project->project_type_id=$project_type_id;

        $project->market_price=$market_price;

        $project->gross=$gross;

        $project->cover_img=$cover_img;

        $project->save();



        //图片处理

        imgReplaceAct($cover_img,$pre_cover_img);

        return $this->success('保存成功');

    }



    public function del()

    {

        $project_id = $this->request->param('project_id/d',0);

        if($project_id==0) return $this->error($this->name.'不存在');



        if(null === $project = ProjectModel::find($project_id)) return $this->error($this->name.'不存在');

        if($project->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

        if($project->is_delete != 0) return $this->error($this->name.'不存在');



        //判断抢购 后期补充



        //判断次卡 后期补充



        $project->is_delete =1 ;

        $project->save();

        return $this->success('删除成功');



    }





    /********************************************************* 项目分类 *****************************************************************/

    public function getProjectTypeList()

    {

        $name=$this->request->param('name');

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0]

        ];

        if(!empty($name)) $where[]=['name','=',$name];

        $project_type_model = new ProjectTypeModel();

        $project_type_model=$project_type_model->withCount(['project'=>function($query){

            $query->where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0]

            ]);

        }]);

        $order=['sort'=>'desc','add_time'=>'desc'];

        $field='*';

        $append=['add_time_format'];

        $return = DataLogic::getDataList($project_type_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }



    public function showProjectType()

    {

        $project_type_id=$this->request->param('project_type_id/d',0);

        if($project_type_id==0) return $this->success([]);



        if(null === $project_type = ProjectTypeModel::find($project_type_id)) return $this->error('分类不存在');

        if($project_type->shop_id != $this->request->shopId) return $this->error('分类不存在');

        if($project_type->is_delete != 0) return $this->error('分类不存在');



        $detail=[

            'name'=>$project_type->name,

            'sort'=>$project_type->sort,

        ];



        return $this->success(['detail'=>$detail]);

    }



    public function saveProjectType()

    {

        $project_type_id=$this->request->param('project_type_id/d',0);

        $name=$this->request->param('name');

        $sort=$this->request->param('sort/d',0);

        if(empty($name)) return $this->error('请填写分类名称');

        if($sort<0) return $this->error('请填写排序，且不能小于0');

        if($project_type_id != 0){

            if(null === $project_type = ProjectTypeModel::find($project_type_id)) return $this->error('分类不存在');

            if($project_type->shop_id != $this->request->shopId) return $this->error('分类不存在');

            if($project_type->is_delete != 0) return $this->error('分类不存在');



            if($project_type->name != $name){

                if(null !== ProjectTypeModel::where([

                        ['shop_id','=',$this->request->shopId],

                        ['is_delete','=',0],

                        ['name','=',$name]

                    ])->find()){

                    return $this->error('该分类名称已存在，请重新输入');

                }

            }

        }else{

            if(null !== ProjectTypeModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['is_delete','=',0],

                    ['name','=',$name]

                ])->find()){

                return $this->error('该分类名称已存在，请重新输入');

            }

            $project_type = new ProjectTypeModel();

            $project_type->shop_id= $this->request->shopId;

            //创建“项目”分类开始
            $goods_category =  GoodsCategoryModel::where('name','项目')
                ->where('shop_id',$this->request->shopId)
                ->where('is_delete',0)
                ->find();

            if($goods_category == null){
                $goods_category = new GoodsCategoryModel();
                $goods_category->shop_id = $this->request->shopId;
                $goods_category->name='项目';
                $goods_category->pid=0;
                $goods_category->icon='';
                $goods_category->sort=1;
                $goods_category->save();
            }
            //项目名称：
            $goods_sub_category_name = $name.'(项目)';

            $goods_sub_category =  GoodsCategoryModel::where('name',$goods_sub_category_name)
                ->where('shop_id',$this->request->shopId)
                ->where('is_delete',0)
                ->find();

            if($goods_sub_category  == null){
                $sub_category = new GoodsCategoryModel();
                $sub_category->shop_id = $this->request->shopId;
                $sub_category->name=$goods_sub_category_name;
                $sub_category->pid=$goods_category->category_id;
                $sub_category->icon='';
                $sub_category->sort=1;
                $sub_category->save();
            }
            //创建“项目”分类结束

        }

        $project_type->name=$name;

        $project_type->sort=$sort;

        $project_type->save();



        return $this->success('操作成功');



    }



    public function delProjectType()

    {

        $project_type_id=$this->request->param('project_type_id/d',0);

        if($project_type_id==0) return $this->error('分类不存在');



        if(null === $project_type = ProjectTypeModel::find($project_type_id)) return $this->error('分类不存在');

        if($project_type->shop_id != $this->request->shopId) return $this->error('分类不存在');

        if($project_type->is_delete != 0) return $this->error('分类不存在');



        $project_count= ProjectModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['project_type_id','=',$project_type_id]

        ])->count() ?? 0;



        if($project_count!=0) return $this->error('分类下面还有项目，无法删除');

        $project_type->is_delete = 1;

        $project_type->save();

        return $this->success('删除成功');

    }

}

